/*
	Part of Zombie Navigator
	Copyright © 2015  Zombie Navigator Developers
	
	Display what the Zombie script is doing
*/

var sectionElement = document.getElementById('main');
sectionElement.innerHTML = '';
var ulElement = sectionElement.appendChild(document.createElement('ul'));

const printEvent = message => {
	if ('object' != typeof message) {
		message = Object.create(null);
	}
	
	var liElement = ulElement.appendChild(document.createElement('li'));
	
	var subject = 'Event';
	
	switch (message.type) {
		case 'open':
			subject = 'Opened';
			break;
		
		case 'ready':
			subject = 'Loaded';
			break;
		
		case 'close':
			subject = 'Closed';
			break;
		
		case 'controllerInfo':
			subject = 'Running script';
			break;
		
		case 'systemInfo':
			subject = 'New window';
			break;
		
	}
	
	liElement.classList.add('event-' + message.type);
	
	if (message.isPrivate) {
		subject += ' (private)';
		liElement.classList.add('private');
	}
	
	
	var codeElement1 = liElement.appendChild(document.createElement('code'));
	codeElement1.classList.add('date');
	codeElement1.appendChild(document.createTextNode(`[${(new Date).toISOString()}]`));
	liElement.appendChild(document.createTextNode(' '));
	
	var strongElement = liElement.appendChild(document.createElement('strong'));
	strongElement.appendChild(document.createTextNode(subject));
	liElement.appendChild(document.createTextNode(' '));
	
	if (!message.withoutUri) {
		var codeElement2 = liElement.appendChild(document.createElement('code'));
		if (message.uri) {
			var aElement = document.createElement('a');
			aElement.href = message.uri;
			aElement.appendChild(document.createTextNode(message.uri));
			codeElement2.appendChild(aElement);
		} else {
			codeElement2.appendChild(document.createTextNode('(New window)'));
		}
	}
	
	if (message.platform) {
		var platform = message.platform + '';
		if (message.platformVersion) {
			platform += ' ' + message.platformVersion;
		}
		liElement.appendChild(document.createTextNode(' on '));
		var spanElement = document.createElement('span');
		spanElement.appendChild(document.createTextNode('' + platform));
		spanElement.title = navigator.userAgent;
		liElement.appendChild(spanElement);
	}
	
	if (message.system) {
		liElement.appendChild(document.createTextNode(` (${message.system})`));
	}
	
	if (message.addon && message.addonVersion) {
		liElement.appendChild(document.createTextNode(` with ${message.addon} ${message.addonVersion}`));
	}
};


/*
	Receive messages from the content script
*/
window.addEventListener('message', event => {
	printEvent(event.data);
}, false);


// vim: ts=4 noet ai

